var url = null;

$(function() {
	loadStatus();
});

function loadStatus() {
	$('#status_query').combobox({
		url : getHttpRequestPath("/warehouse/loadStatus"),
		valueField : 'value',
		textField : 'name'
	});
}

function loadSku() {
	$('#sku_text').combobox({
		url : getHttpRequestPath("/baseInfo/loadSku"),
		valueField : 'value',
		textField : 'name'
	});
}

/**
 * 查询
 */
function query() {
	var cmd = new Cmd();
	var incomeId = $("#incomeId_query").val();
	cmd.incomeId = incomeId.length > 0 ? incomeId : null;
	var statusCode = $("#status_query").combobox('getValue');
	cmd.statusCode = statusCode.length > 0 ? new Number(statusCode) : 0;
	
	$('#dg').datagrid({
		url : getHttpRequestPath("/warehouse/queryIncomeList"),
		pageNumber : 1,
		queryParams : {
			cmd : JSON.stringify(cmd, cmd.StringifyReplacer)
		}
	});
}

/**
 * 增加入库单
 */
function initAdd() {
	url = getHttpRequestPath("/warehouse/addIncome");
	clearData();
	loadSku();
	$('#dlg').dialog({
		modal : true
	}).dialog('open').dialog('setTitle', '增加入库单');
}

/**
 * 修改入库单
 */
function initEdit(incomeId) {
	url = getHttpRequestPath("/warehouse/editIncome");
	clearData();
	loadSku();
	getIncome(incomeId);
	$('#dlg').dialog({
		modal : true
	}).dialog('open').dialog('setTitle', '修改入库单' + incomeId);
}

function clearData() {
	$('#incomeId_text').val("");
	$('#sku_text').combobox("setValue", "");
	$('#amount_text').numberbox("setValue", "");
}

/**
 * 初始化表单数据
 * 
 * @param row
 */
function initIncome(income) {
	$('#incomeId_text').val(income.incomeId);
	$('#sku_text').combobox("setValue", income.sku);
	$('#amount_text').numberbox("setValue", income.amount);
}

function formatProduct(val, row, index) {
	var str = "";
	str += row.sku + " * " + row.amount;
	return str;
}

var formatDateStr = "yyyy-MM-dd hh:mm:ss";
function formatDate(val, row, index) {
	var str ="";
	str += new Date(row.create.time).format(formatDateStr) + "<br>";
	if (row.complete.time == "Jan 1, 1970 8:00:00 AM") {
		str += "-";
	} else {
		str += new Date(row.complete.time).format(formatDateStr);
	}
	return str;
}

/**
 * 操作
 */
function formatAction(val, row, index) {
	var str = "";
	if (row.status == 'CREATED') {
		str += createLinkElement('编辑', 'initEdit(\'' + row.incomeId + '\')', 'icon-edit');
		str += createLinkElement('入库', 'incomeOrder(\'' + row.incomeId + '\')', 'icon-edit');
		str += createLinkElement('取消', 'cancel(\'' + row.incomeId + '\')', 'icon-cancel');
	}
	return str;
}
